package com.zg.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zg.pojo.LiveStream;
import com.zg.service.LiveStreamService;

@Controller
@RequestMapping("/livestream")
public class LiveStreamController {
	@Resource
	private LiveStreamService liveStreamService;
	
	@RequestMapping(value="/addlivestream", method=RequestMethod.GET)
	public String addLiveStreamForm(HttpServletRequest request, Model model) {
		return "livestream/addLiveStream";
	}
	
	@RequestMapping(value="/addlivestream", method=RequestMethod.POST)
	public String addLiveStream(HttpServletRequest request, Model model){
		String name = new String(request.getParameter("name"));
		String url = new String(request.getParameter("url"));
		int provider = 1;
		
		LiveStream liveStream = new LiveStream();
		
		liveStream.setName(name);
		liveStream.setUrl(url);
		liveStream.setProviderLivestream(provider);
		
		this.liveStreamService.insert(liveStream);
		
		return "redirect:/livestream/listlivestream";
	}
	
	@RequestMapping(value="listlivestream")
	public String listLiveStream(HttpServletRequest request, Model model){
		List<LiveStream> liveStreams;
		liveStreams = this.liveStreamService.getAll();
		model.addAttribute("list", liveStreams);
		return "livestream/listLiveStream";
	}
	
	@RequestMapping(value="editlivestream", method = RequestMethod.GET)
	public String editLiveStreamForm(HttpServletRequest request, Model model){
		int id = new Integer(request.getParameter("id"));
		LiveStream liveStream = new LiveStream();
		liveStream = this.liveStreamService.getLiveStreamById(id);
		model.addAttribute("livestream", liveStream);
		return "livestream/editLiveStream";
	}
	
	@RequestMapping(value="editlivestream", method = RequestMethod.POST)
	public String editLiveStream(HttpServletRequest request, Model model) {
		int id = new Integer(request.getParameter("id"));
		String name = new String(request.getParameter("name"));
		String url = new String(request.getParameter("url"));		
		
		LiveStream liveStream = new LiveStream();
		
		liveStream = this.liveStreamService.getLiveStreamById(id);
		
		liveStream.setName(name);
		liveStream.setUrl(url);
		
		this.liveStreamService.updateLiveStreamByPrimaryKeySelective(liveStream);
		
		return "redirect:/livestream/listlivestream";
	}
}
